<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\notice;

use app\model\mysql\NoticeModel;
use app\service\BaseService;

final class NoticeConsumerService extends BaseService
{
    /**
     * @var NoticeModel 提醒模型
     */
    private NoticeModel $model;

    public function __construct(NoticeModel $model)
    {
        $this->model = $model;
    }

    /**
     * 发送提醒.
     */
    public function sendNotice(): bool
    {
        $notice = NoticeService::getInstance(intval($this->model->getAttr('type')));
        if (! $notice) {
            return false;
        }
        $staffIds = json_decode($this->model->getAttr('staff_ids'), true);
        if ($notice->sendStaffMsg($staffIds, $this->model->getAttr('content'))) {
            $this->model->setAttr('status', NoticeModel::STATUS_SUCCESS);
            return $this->model->save();
        }
        $this->model->setAtrr('status', NoticeModel::STATUS_FAIL);
        $this->model->setAtrr('msg', self::getError(0));
        $this->model->save();
        return false;
    }
}
